library(data.table)
library(lubridate)
library(stringi)
library(stringr)
library(ggplot2)
library(ggthemes)
library(scales)
library(showtext)

# Declare working directory beforehand in an environment variable
# IMPERIAL_LEGAL_POLITICS_REPLICATION_PATH = "path_to_your_folder"
# with the aid of usethis::edit_r_environ()
# Restart R session for the changes to take effect
path <- Sys.getenv("IMPERIAL_LEGAL_POLITICS_REPLICATION_PATH")
setwd(path)

# Load an object with case-instance-side-outcome-level data
load("data/crimea_case_outcomes_side_instance.rdata")

# Font for plotting
font_name <- "Arial"
showtext_auto()

##############################
# Aggregate data at case level (with first instance taking precedence)

cols_to_keep <- c("caseid", "caseTypeCode", "caseCategoryUnified", "caseCategoryRedux", "caseCategory_government", "caseCategory_private", "year", "year_factor", "petty_case", "countDocumentsByCourt", "lncountDocumentsByCourt", "days_elapsed", "claimSum_deflated", "claimSum_decile", "recoverySum_deflated", "was_appealed", "registrationDate", "date", "dispute_type", "local_entities_only", names(crimea_case_outcomes_side_instance)[grepl("judge|government|agency|local_entity", names(crimea_case_outcomes_side_instance))])

crimea_cases <- crimea_case_outcomes_side_instance[, lapply(.SD, function(x) x[[1]]), by = "caseid", .SDcols = cols_to_keep ]
crimea_cases[, c("government", "federal_agency", "regional_agency", "municipal_agency", "local_entity") := NULL ]

##############################
# FIGURE 1. Annual count of Government vs. Private disputes, Crimea and Sevastopol arbitrazh courts of first instance in 2014–2019

government_year_freqs_all_long <- crimea_cases[ involved_government == 1, .N, by = c("government_type", "year")]
government_year_freqs_all_long[, type := "a) All cases vs Government"]

government_year_freqs_admin_long <- crimea_cases[ involved_government == 1 & caseTypeCode == "administrative", .N, by = c("government_type", "year")]
government_year_freqs_admin_long[, type := "b) Administrative cases vs Government"]

government_year_freqs_civil_long <- crimea_cases[ involved_government == 1 & caseTypeCode == "civil", .N, by = c("government_type", "year")]
government_year_freqs_civil_long[, type := "c) Civil cases vs Government"]

government_year_freqs_long <- rbind(government_year_freqs_all_long, government_year_freqs_admin_long)
government_year_freqs_long <- rbind(government_year_freqs_long, government_year_freqs_civil_long)

government_year_freqs_plot <- ggplot(aes(x = year, y= N, fill = government_type), data = government_year_freqs_long) + 
	geom_area() +
	labs(y = "Annual count of cases registered by commercial courts in Crimea\nSevastopol w. Government as plaintiff/respondent vs Private entity", x = "", fill = "") +
	scale_x_continuous(expand = c(0.01, 0)) +
	facet_wrap(.~type, scales = "free_y", nrow = 3) +
	scale_fill_manual(values = c("grey70", "grey50", "grey30")) +
	theme_minimal() +
	theme(legend.position = "bottom", text = element_text(family = font_name, size = 16), legend.text=element_text(size = 18))
ggsave(government_year_freqs_plot, file = "figures/figure1_government_year_freqs_plot.pdf", device = cairo_pdf, width = 8.75, height = 7)

##############################
# FIGURE S2. Annual composition of Federal agency vs. Private disputes by agency type, Crimea and Sevastopol arbitrazh courts of first instance in 2014–2019

count_cases_by_federal_agency <- crimea_case_outcomes_side_instance[involved_federal_agency == 1, list( N = uniqueN(caseid) ), by = c("federal_agency_type", "year") ]

# Fill in other category with small agencies
count_cases_by_federal_agency[ federal_agency_type %in% c(NA, "rosprirodnadzor", "cb", "rosgvardia", "rosimushhestvo", "emercom", "rostekhnadzor", "roszdravnadzor", "roskaznachejstvo", "rosgidromet", "rosrybolovstvo", "rosstandart", "minust", "rospotrebnadzor", "fms", "rosfinmonitoring", "rosselhoznadzor", "rosstat", "fss", "fsin"), federal_agency_type := "other"]
count_cases_by_federal_agency <- count_cases_by_federal_agency[, list( N = sum(N) ), by = c("federal_agency_type", "year")]

# Compute shares
totals_by_year <- count_cases_by_federal_agency[, list(yearly_total = sum(N)), by = "year"]
count_cases_by_federal_agency <- merge(count_cases_by_federal_agency, totals_by_year, by = "year", all = T)
count_cases_by_federal_agency[, share := N/yearly_total]
# Add zeros
all_combs <- CJ(federal_agency_type = unique(count_cases_by_federal_agency$federal_agency_type), year = unique(count_cases_by_federal_agency$year))
count_cases_by_federal_agency <- merge(all_combs, count_cases_by_federal_agency, by = c("federal_agency_type", "year"), all = T)
count_cases_by_federal_agency[ is.na(N), N := 0 ]
count_cases_by_federal_agency[ is.na(share), share := 0 ] 

# Rename levels
count_cases_by_federal_agency[, federal_agency_type := as.factor(federal_agency_type)]
levels(count_cases_by_federal_agency$federal_agency_type) <- c("Antitrust", "Tax", "Bailiffs", "Customs", "Police", "Other", "Pension fund", "Alcohol regulator", "Communications regulator", "Transport regulator")

# Plot
cases_by_federal_agency_plot <- ggplot(aes(x = year, y = share, fill = federal_agency_type), data = count_cases_by_federal_agency) + 
	geom_bar(stat = "identity") +
	labs(y = "Share of cases with given federal agency as party\nout of all federal agency vs. private entity cases", x = "", fill = "") +
	scale_x_continuous(expand = c(0.01, 0)) +
	scale_y_continuous(label = function(x) scales::percent(x, accuracy = 1)) +
	theme_minimal() +
	theme(legend.position = "bottom", text = element_text(family = font_name, size = 12), legend.text=element_text(size = 14))
ggsave(cases_by_federal_agency_plot, file = "figures/figureS2_cases_by_federal_agency_plot.pdf", device = cairo_pdf, width = 8.75, height = 8.1)
